package m.jclecai.com.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.ui.BetSSQ_DLT_QLCActivity;
import m.jclecai.com.ui.BetSYXWActivity;
import m.jclecai.com.utils.BaseUtils;

public class NewWebViewHelperActivity extends BaseActivity {

    @BindView(R.id.webview)
    public WebView web_tendecy_view;

    @BindView(R.id.pb_progress)
    public ProgressBar pbProgress;


    @Override
    protected int getContentView() {
        return R.layout.activity_new_web_view_helper;
    }


    @Override
    public void initData() {

        String url=getIntent().getStringExtra("url");
        setTitle(getIntent().getStringExtra("title"));
        leftOptionText("");
        //webView的设置
        WebSettings webSettings = web_tendecy_view.getSettings();

        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setUseWideViewPort(true);//适应分辨率
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUserAgentString(webSettings.getUserAgentString());

        webSettings.setDatabaseEnabled(true);
        webSettings.setGeolocationEnabled(true);
        String dir = getDir("database", Context.MODE_PRIVATE).getPath();
        webSettings.setDatabasePath(dir);
        webSettings.setGeolocationDatabasePath(dir);

        webSettings.setAppCacheEnabled(true);
        String cacheDir = getDir("cache", Context.MODE_PRIVATE).getPath();
        webSettings.setAppCachePath(cacheDir);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setAppCacheMaxSize(1024 * 1024 * 10);
        webSettings.setAllowFileAccess(true);

        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);

        webSettings.setBuiltInZoomControls(false);
        //  webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setSupportZoom(false);
        webSettings.setDisplayZoomControls(false);
        webSettings.setDefaultTextEncodingName("utf-8");

        web_tendecy_view.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                web_tendecy_view.getSettings().setBlockNetworkImage(false);
                super.onPageFinished(view, url);
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(url.contains("dlt")){

                    Intent intent = new Intent(NewWebViewHelperActivity.this, BetSSQ_DLT_QLCActivity.class);
                    intent.putExtra("type", "dlt");
                    startActivity(intent);

                    finish();
                    return true;
                }else if(url.contains("zjsyxw")){
                    //这里进行一个跳转；

                    Intent intent = new Intent(NewWebViewHelperActivity.this,  BetSYXWActivity.class);
                    intent.putExtra("type", "zjsyxw");
                    startActivity(intent);

                    finish();
                    return true;
                }else if(url.contains("jczq")){
                    BaseUtils.startAcy(NewWebViewHelperActivity.this, LotteryJCZQActivity.class);
                    finish();
                    return true;
                }else if(url.contains("jzlq")){
                    BaseUtils.startAcy(NewWebViewHelperActivity.this, LotteryJCLQActivity.class);
                    finish();
                    return true;
                }else if(url.contains("ssq")){
                    Intent intent = new Intent(NewWebViewHelperActivity.this, BetSSQ_DLT_QLCActivity.class);
                    intent.putExtra("type", "ssq");
                    startActivity(intent);
                    finish();
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        //去掉webView右边的滚动条
        web_tendecy_view.setHorizontalScrollBarEnabled(false);//水平不显示
        web_tendecy_view.setVerticalScrollBarEnabled(false); //垂直不显示
        web_tendecy_view.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成
                    pbProgress.setVisibility(View.GONE);
                } else {
                    // 加载中
                    pbProgress.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        //加载url
            web_tendecy_view.loadUrl(url);
    }

    public static Intent getIntent(Context context, String url,String title){

        Intent intent = new Intent(context,NewWebViewHelperActivity.class);
        intent.putExtra("url",url);
        intent.putExtra("title",title);
        return intent;
    }
}
